{
  TChain *in = new TChain("is430");
  in->Add("data/22Ne_on_dPE/Trees/new.root");
  if (0 == in->GetEntries()) {
    std::cerr << "Problem opening input file.\n";
    return;
  }

  float in_ge_e;
  float in_ge_dc_e;
  Long64_t in_ge_t;
  UShort_t in_ge_id;
  in->SetBranchAddress("ge_e", &in_ge_e);
  in->SetBranchAddress("ge_dc_e", &in_ge_dc_e);
  in->SetBranchAddress("ge_t", &in_ge_t);
  in->SetBranchAddress("ge_id", &in_ge_id);

  float in_fb_de;
  float in_fb_e;
  Long64_t in_fb_t;
  UChar_t in_fb_det_id;
  UChar_t in_fb_strip_id;
  float in_fb_strip_pos;
  float in_fb_theta;
  float in_fb_phi;
  float in_fb_p_de;
  float in_fb_p_e;
  in->SetBranchAddress("fb_de", &in_fb_de);
  in->SetBranchAddress("fb_e", &in_fb_e);
  in->SetBranchAddress("fb_t", &in_fb_t);
  in->SetBranchAddress("fb_det_id", &in_fb_det_id);
  in->SetBranchAddress("fb_strip_id", &in_fb_strip_id);
  in->SetBranchAddress("fb_strip_pos", &in_fb_strip_pos);
  in->SetBranchAddress("fb_theta", &in_fb_theta);
  in->SetBranchAddress("fb_phi", &in_fb_phi);
  in->SetBranchAddress("fb_p_de", &in_fb_p_de);
  in->SetBranchAddress("fb_p_e", &in_fb_p_e);

  TFile file_out("data/22Ne_on_dPE/Trees/new_precond.root", "recreate");
  if (!file_out.IsOpen()) {
    std::cerr << "Problem opening output file.\n";
    return;
  }
  TTree *out = new TTree("is430", "is430");

  float ge_e;
  UShort_t ge_id;
  out->Branch("ge_e", &ge_e, "ge_e/F");
  out->Branch("ge_id", &ge_id, "ge_id/s");

  float fb_beta;
  float fb_gamma;
  float fb_x;
  float fb_y;
  float fb_z;
  out->Branch("fb_beta", &fb_beta, "fb_beta/F");
  out->Branch("fb_gamma", &fb_gamma, "fb_gamma/F");
  out->Branch("fb_x", &fb_x, "fb_x/F");
  out->Branch("fb_y", &fb_y, "fb_y/F");
  out->Branch("fb_z", &fb_z, "fb_z/F");

  int entry_num = in->GetEntries();
std::cout << entry_num << '\n';
  for (int i = 0; entry_num > i; ++i) {
    in->GetEntry(i);
//std::cout << in_fb_t << ' ' << in_ge_t << std::endl;
    if (87.5<abs(in_fb_t-in_ge_t-300)) {
      continue;
    }
    if (500>in_fb_e) {
      continue;
    }
    if (!(in_fb_de<3.7029e7/(in_fb_e+5714.3) &&
        in_fb_de>1.0338e7/(in_fb_e+2135.1))) {
      continue;
    }
    // 950 - 1016 - 1070.
    if (900>in_ge_e || 1140<in_ge_e) {
      continue;
    }

    // Transform proton angles to neon angles.
    float const kPi = 3.14159265;
    float t = in_fb_theta / kPi * 180.0;
    float ne_theta = (
        1.2575e-01 + (
        5.6046e-04 + (
        -4.7355e-05 + (
        5.0427e-07 + (
        -2.1773e-09 + 
        3.4529e-12 * t
        ) * t
        ) * t
        ) * t
        ) * t
        ) * t * kPi / 180.0;
    float ne_phi = fmod(in_fb_phi + kPi, 2 * kPi);
    float ne_e =
        1.7510e+00 + (
        -4.7549e-04 + (
        1.7075e-04 + (
        -1.5010e-06 +
        3.7628e-09 * t
        ) * t
        ) * t
        ) * t;

    ge_e = in_ge_e;
    ge_id = in_ge_id;
    fb_gamma = ne_e / 941.0 + 1;
    fb_beta = sqrt(1 - 1 / (fb_gamma * fb_gamma));
    fb_x = -sin(ne_phi) * sin(ne_theta);
    fb_y = cos(ne_phi) * sin(ne_theta);
    fb_z = cos(ne_theta);

    out->Fill();
  }
  file_out.Write();
  file_out.Close();
}
